package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.ExpertDataVo;
import com.ruoyi.system.domain.TenderItem;
import com.ruoyi.system.mapper.TenderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ExpertScoringMapper;
import com.ruoyi.system.domain.ExpertScoring;
import com.ruoyi.system.service.IExpertScoringService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.ObjectUtils;

/**
 * 专家评分Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@Service
public class ExpertScoringServiceImpl implements IExpertScoringService
{
    @Autowired
    private ExpertScoringMapper expertScoringMapper;
    @Autowired
    private TenderItemMapper tenderItemMapper;

    /**
     * 查询专家评分
     * 
     * @param expertId 专家评分主键
     * @return 专家评分
     */
    @Override
    public ExpertScoring selectExpertScoringByExpertId(Long expertId)
    {
        return expertScoringMapper.selectExpertScoringByExpertId(expertId);
    }

    /**
     * 查询专家评分列表
     * 
     * @param expertScoring 专家评分
     * @return 专家评分
     */
    @Override
    public List<ExpertScoring> selectExpertScoringList(ExpertScoring expertScoring)
    {
        return expertScoringMapper.selectExpertScoringList(expertScoring);
    }

    /**
     * 新增专家评分
     * 
     * @param expertScoring 专家评分
     * @return 结果
     */
    @Override
    public int insertExpertScoring(ExpertScoring expertScoring)
    {
        return expertScoringMapper.insertExpertScoring(expertScoring);
    }

    /**
     * 修改专家评分
     * 
     * @param expertScoring 专家评分
     * @return 结果
     */
    @Override
    public int updateExpertScoring(ExpertScoring expertScoring)
    {
        expertScoring.setUpdateTime(DateUtils.getNowDate());
        return expertScoringMapper.updateExpertScoring(expertScoring);
    }

    /**
     * 批量删除专家评分
     * 
     * @param expertIds 需要删除的专家评分主键
     * @return 结果
     */
    @Override
    public int deleteExpertScoringByExpertIds(String expertIds)
    {
        return expertScoringMapper.deleteExpertScoringByExpertIds(Convert.toStrArray(expertIds));
    }

    /**
     * 删除专家评分信息
     * 
     * @param expertId 专家评分主键
     * @return 结果
     */
    @Override
    public int deleteExpertScoringByExpertId(Long expertId)
    {
        return expertScoringMapper.deleteExpertScoringByExpertId(expertId);
    }

    @Override
    public int insertData(ExpertDataVo vo) {
        int res = 0;
        //项目id
        String itemId = vo.getItemId();
        //获取专家id
        SysUser sysUser = ShiroUtils.getSysUser();
        Long userId = sysUser.getUserId();
        //获取供应商id
        Long suppilerId = 1L;
        TenderItem tenderItem = tenderItemMapper.selectTenderItemByItemId(Long.valueOf(itemId));
        if(!ObjectUtils.isEmpty(tenderItem)){
            suppilerId = tenderItem.getSupplierId();
        }
        //增加判断，如果相同项目id，供应商id，专家id，把之前的数据删除，重新新增评分结果的内容
        int count = expertScoringMapper.selectDataByIds(Long.valueOf(itemId),userId,suppilerId);
        if(count>0){
            expertScoringMapper.deleteDataByIds(Long.valueOf(itemId),userId,suppilerId);
        }
        //评分项数据集合
        String dataList = vo.getDataList();
        JSONArray arr = JSON.parseArray(dataList);
        for (int i = 0; i < arr.size(); i++) {
            JSONObject item = arr.getJSONObject(i);
            ExpertScoring expertScoring = new ExpertScoring();
            //项目id
            expertScoring.setItemsId(Long.valueOf(itemId));
            //供应商id
            expertScoring.setSupplierId(suppilerId);
            //专家id
            expertScoring.setUserId(userId);
            expertScoring.setFirstLevelCategory(item.getString("firstLevelCategory"));
            expertScoring.setSecondLevelCategory(item.getString("secondLevelCategory"));
            expertScoring.setItemName(item.getString("itemName"));
            expertScoring.setScoringMethod(item.getString("scoringMethod"));
            expertScoring.setScoreSet(item.getString("scoreSet"));
            expertScoring.setWeight(item.getString("weight"));
            expertScoring.setIsObjective(item.getLong(("isObjective")));
            expertScoring.setItemBz(item.getString("itemBz"));
            expertScoring.setScoreYj(item.getString("scoreYj"));
            res = expertScoringMapper.insertExpertScoring(expertScoring);
            System.out.println(item);
        }
        return res;
    }



    @Override
    public List<ExpertScoring> selectByItemId(Long itemId) {
        return expertScoringMapper.selectByItemId(itemId);
    }
}
